package com.example.patient.service.impl;

import com.example.common.entity.SysDiseaseEncyclopedia;
import com.example.common.entity.SysDiseaseEncyclopediaClassification;
import com.example.common.support.ApiException;
import com.example.common.utils.CheckUtils;
import com.example.patient.dto.resp.DiseaseListResp;
import com.example.patient.jpa.SysDiseaseEncyclopediaClassificationJpa;
import com.example.patient.jpa.SysDiseaseEncyclopediaJpa;
import com.example.patient.mapper.SysDiseaseEncyclopediaMapper;
import com.example.patient.service.CyclopediaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName CyclopediaServiceImpl
 * @Author wengjt
 * @Date 2021/4/2015:57
 * @Description
 **/
@Service
@Slf4j
public class CyclopediaServiceImpl implements CyclopediaService {
    @Resource
    private SysDiseaseEncyclopediaClassificationJpa sysDiseaseEncyclopediaClassificationJpa;
    @Resource
    private SysDiseaseEncyclopediaJpa sysDiseaseEncyclopediaJpa;
    @Resource
    private SysDiseaseEncyclopediaMapper sysDiseaseEncyclopediaMapper;
    /**
     * 获取疾病类型列表
     *
     * @return
     * @Author wengjt
     * @Date 15:57 2021/4/20
     */
    @Override
    public List<SysDiseaseEncyclopediaClassification> getIllnessTypeList() {
        List<SysDiseaseEncyclopediaClassification> list = sysDiseaseEncyclopediaClassificationJpa.findAll();
        return list;
    }

    /***
     *获取疾病列表
     *
     * @Author wengjt
     * @Date 15:58 2021/4/20
     * @return
     */
    @Override
    public DiseaseListResp getDiseaseList(Integer page, Integer limit, Integer decId) {
        CheckUtils.checkNull(page, new ApiException(1, "页码不能为空"));
        CheckUtils.checkNull(limit, new ApiException(1, "单页大小不能为空"));
        CheckUtils.checkNull(decId, new ApiException(1, "参数有误"));
        DiseaseListResp resp = new DiseaseListResp();
        Integer skip = (page - 1) * limit;
        List<SysDiseaseEncyclopedia> list = sysDiseaseEncyclopediaJpa.findByDecIdPage(decId, skip, limit);
        Integer total = sysDiseaseEncyclopediaJpa.countByDecId(decId);
        List<DiseaseListResp.Disease> collect = list.stream().map(d -> {
            DiseaseListResp.Disease disease = new DiseaseListResp.Disease();
            disease.setDeId(d.getDeId());
            disease.setName(d.getName());
            return disease;
        }).collect(Collectors.toList());
        resp.setList(collect);
        resp.setTotal(total);
        return resp;
    }
    /***
     *根据疾病名搜索疾病百科列表
     *    
     * @Author wengjt
     * @Date 14:18 2021/4/22
     */
    @Override
    public DiseaseListResp searchDiseaseList(Integer page, Integer limit, String name) {
        CheckUtils.checkNull(page, new ApiException(1, "页码不能为空"));
        CheckUtils.checkNull(limit, new ApiException(1, "单页大小不能为空"));
        DiseaseListResp resp = new DiseaseListResp();
        Integer skip = (page - 1) * limit;
        List<SysDiseaseEncyclopedia> sysDiseaseEncyclopedias = sysDiseaseEncyclopediaMapper.searchByName(name, skip, limit);
        Integer total = sysDiseaseEncyclopediaMapper.CountByName(name);
        List<DiseaseListResp.Disease> collect = sysDiseaseEncyclopedias.stream().map(d -> {
            DiseaseListResp.Disease disease = new DiseaseListResp.Disease();
            disease.setDeId(d.getDeId());
            disease.setName(d.getName());
            return disease;
        }).collect(Collectors.toList());
        resp.setList(collect);
        resp.setTotal(total);
        return resp;
    }

    /**
     * 获取疾病详情
     *
     * @return
     * @Author wengjt
     * @Date 15:58 2021/4/20
     */
    @Override
    public SysDiseaseEncyclopedia getDiseaseInfo(Integer deId) {
        CheckUtils.checkNull(deId, new ApiException(1, "疾病id不能为空"));
        SysDiseaseEncyclopedia byDeId = sysDiseaseEncyclopediaJpa.findByDeId(deId);
        return byDeId;
    }
}
